<?php
    include_once ("feedParser.php");
    function sqlRequest($request)
    {
        var_dump($request);
        $mysqli = new mysqli("localhost", "root", "root", "rssBdd");
        if ($mysqli->connect_errno)
        {
            echo htmlentities('Échec de la connexion : ' . $mysqli->error);
            exit;
        }
        $requete = $request;
        if ($result = $mysqli->query($requete))
        {			
            echo htmlentities('Requête effectuée.') . '<br/>';
            $mysqli->close();
            return $result;
        }
        else 
        {
            echo htmlentities('La requête a echoué : ' . $mysqli->error);
            $mysqli->close();
        }
        
    }

    function addFeed($feedItem)
    {
        sqlRequest('INSERT INTO feeds (title_f, description_f, link_f, lastBuildDate_f, pubdate_f, ttl_f)
        VALUES ("' . addslashes($feedItem->getTitleFeed()) . '", "' . addslashes($feedItem->getDescriptionFeed()) . '", "' . addslashes($feedItem->getLinkFeed()) . '", "' . addslashes($feedItem->getLastBuildDate()) . '", "' . addslashes($feedItem->getPubDateFeed()) . '", "' . addslashes($feedItem->getTtlFeed()) . '")');
    }

    //Tester
    //$test = new feedParser("http://lequipe.fr/rss/actu_rss_Football.xml");
    //addFeed($test);

    function displayFeed($number)
    {
        $feedItem = sqlRequest('SELECT * FROM feeds WHERE id_f = ' . $number . ';');
        $feedArray = $feedItem->fetch_assoc();
        echo '<div class="news">';
        echo '<div class="newsTitle"><a href="' . htmlentities($feedArray['link_f']) . '">' . htmlentities($feedArray['title_f']) . '</a></div>';
        echo '<div class="newsDescription">' . htmlentities($feedArray['description_f']) . '</div>';
        echo '<div class="newsLink"><a href ="' . htmlentities($feedArray['link_f']) . '">Voir site >></a></div>';
        echo '<div class="newsDate">' . htmlentities($feedArray['lastBuildDate_f']) . '</div></div>';
    }
    
    //Tester
    //displayFeed(1);

    function addNews($newsItem)
    {
        $doesExist = sqlrequest('SELECT * FROM news WHERE guid_n = "' . $newsItem->getGuidNews() . '";');
        if (($doesExist->fetch_assoc()) == null)
        {
            $titleFeed = sqlRequest('SELECT id_f FROM feeds WHERE title_f = "' . $newsItem->getTitleFeed() . '";');
            var_dump($titleFeed->fetch_assoc()['id_f']);
            sqlRequest('INSERT INTO news (id_f, title_n, description_n, link_n, guid_n, pubdate_n)
            VALUES ("' . addslashes($titleFeed->fetch_assoc()['id_f']) . '", "' . addslashes($newsItem->getTitleNews()) . '", "' . addslashes($newsItem->getDescriptionNews()) . '", "' . addslashes($newsItem->getLinkNews()) . '", "' . addslashes($newsItem->getGuidNews()) . '", "' . $newsItem->getPubDateNews() . '");');
        }
    }

    function addAllNews($listNews)
    {
        for ($i = 0; $listNews[$i] != null; $i++)
        {
            addNews($listNews[$i]);
        }

    }

    //Tester
    //addnews($test->listItems[0]);
    
    function displayNews()
    {
        $newsItem = sqlRequest('SELECT * FROM news ORDER BY pubdate_n DESC;');
        while ($newsArray = $newsItem->fetch_assoc())
        {
            echo '<div class="news">';
            echo '<div class="newsTitle"><a href="' . htmlentities($newsArray['link_n']) . '">' . htmlentities($newsArray['title_n']) . '</a></div>';
            echo '<div class="newsDescription">' . htmlentities($newsArray['description_n']) . '</div>';
            echo '<div class="newsLink"><a href ="' . htmlentities($newsArray['link_n']) . '">Voir plus >></a></div>';
            echo '<div class="newsDate">' . htmlentities($newsArray['pubdate_n']) . '</div></div>';
        }
    }

    //Tester
    //displayNews();

?>